package com.ruoyi;

import com.ruoyi.stock2.domain.PriceCompAverage200;
import com.ruoyi.stock2.domain.TRADEDATA200;
import com.ruoyi.stock2.mapper.PriceCompAverage200Mapper;
import com.ruoyi.stock2.mapper.STOCKMapper;
import com.ruoyi.stock2.service.*;
import com.ruoyi.stock2.utils.DateUtils2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Type7toseq2seqPred {
	private static final Logger log = LoggerFactory.getLogger(Type7toseq2seqPred.class);

	@Autowired(required = false)
	ITRADEDATA200Service itradedata200Service;

	@Autowired(required = false)
	PriceCompAverage200Mapper priceCompAverage200Mapper;

	Map map7 = new HashMap(5000);

	//@Test
	public void productTextType7(){
		List<PriceCompAverage200> list7 = priceCompAverage200Mapper.selectBuyTrainByType7();
		log.warn("selectBuyTrainByType7:"+list7.size());
		map7 = getSetType7Num();
		File file = new File("/opt/seq2seq_type7_pre.txt");
		BufferedWriter writer = null;
		try {
			if(!file.exists()){
				file.createNewFile();
			}else{
				file.delete();
				file.createNewFile();
			}
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "utf-8"));

			for(int i = 0 ; i < list7.size() ; i++){
				writer.write(getStrType7(list7.get(i)) + "\r\n");
				writer.flush();
			}

			writer.flush();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException  e) {
				e.printStackTrace();
			}
		}


	}





	public String getStrType7(PriceCompAverage200 priceCompAverage200){
		StringBuilder stringBuilderIt = new StringBuilder();
		TRADEDATA200 tradedata = new TRADEDATA200();
		tradedata.setSymbol(priceCompAverage200.getSymbol());
		tradedata.setDatetime(priceCompAverage200.getF1date());
		List<TRADEDATA200> listinput = itradedata200Service.selectTradedataInput40Days(tradedata);
		List<TRADEDATA200> listtarget = itradedata200Service.selectTradedataTarget40Days(tradedata);

		stringBuilderIt.append(listinput.get(0).getSymbol()).append(",");
		stringBuilderIt.append(DateUtils2.formatDateToString(listinput.get(0).getDatetime(),"yyyyMMdd")).append(",");

		List<BigDecimal> lista = new ArrayList<BigDecimal>();
		for(TRADEDATA200 tradedata1 : listinput){
			lista.add(tradedata1.getCloseprice());
		}
		for(TRADEDATA200 tradedata1 : listtarget){
			lista.add(tradedata1.getCloseprice());
		}
		BigDecimal min = Collections.min(lista);

		for(int j = listinput.size()-1; j>0; j--){
			BigDecimal bigDecimal = listinput.get(j).getCloseprice().divide(min, 3, BigDecimal.ROUND_HALF_UP);

			if(map7.get(String.valueOf(bigDecimal))==null && bigDecimal.compareTo( new BigDecimal( 12.024 ) ) < 0){
				while ( map7.get(String.valueOf(bigDecimal))==null && bigDecimal.compareTo( new BigDecimal( 12.024 ) ) < 0) {
					bigDecimal = bigDecimal.add( new BigDecimal(0.001) ).setScale(3, BigDecimal.ROUND_HALF_UP);
				}
			}else if(map7.get(String.valueOf(bigDecimal))==null && bigDecimal.compareTo( new BigDecimal( 12.024 ) ) > 0){
				bigDecimal = new BigDecimal(12.024 );
			}

			stringBuilderIt.append(bigDecimal).append(" ");
		}
		stringBuilderIt.append(listinput.get(0).getCloseprice().divide(min, 3, BigDecimal.ROUND_HALF_UP));
		stringBuilderIt.append(",").append(stringBuilderIt);

		return stringBuilderIt.toString();
	}




	public Map<String, Integer> getSetType7Num(){

		Path path = Paths.get("/opt/seq2seq_type7_train_Set.txt");
		Charset charset = Charset.forName("utf-8");
		try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
			String line = null;
			while ((line = reader.readLine()) != null) {
				String[] spline = line.split(" ");
				for(String str : spline){
					map7.put(str,1);
				}
			}
		} catch (IOException x) {
			System.err.format("IOException: %s%n", x);
		}
		return map7;
	}
}
